#include<iostream>
using namespace std;
#define ok
#define error
class TreeNode {
public:
	int Data;
	char ch;
	TreeNode* left;
	TreeNode* right;
	TreeNode(int n = 0) {
		Data = n;
		left = right = NULL;
	}
};
TreeNode* made(TreeNode* p1, TreeNode* p2) {
	TreeNode* p = new TreeNode(p1->Data + p2->Data);
	if (p1->Data > p2->Data) {
		p->left = p2;
		p->right = p1;
	}
	else {
		p->left = p1;
		p->right = p2;
	}
	return p;
}
void Sort_Tree(TreeNode** a, int n) {
	for (int i = 0; i < n; i++) {
		for (int j = i; j < n; j++) {
			if (a[i]->Data < a[j]->Data) {
				TreeNode* p = a[i];
				a[i] = a[j];
				a[j] = p;
			}
		}
	}
}
void Decode(TreeNode* p, string str) {
	TreeNode* tmp = p;
	int i = 1;
	string cur = "";
	int len = str.length();
	for (int j = 0; j < len; j++) {
		if (tmp == NULL) {
			i = 0;
			break;
		}
		if (str[j] == '0') {
			tmp = tmp->left;
		}
		else {
			tmp = tmp->right;
		}
		if (tmp->left == NULL && tmp->right == NULL) {
			cur += tmp->ch;
			tmp = p;
		}
	}
	if (tmp != p) {
		i = 0;
	}
	if (i == 1) {
		cout << cur << endl;
	}
	else {
		cout << "error" << endl;
	}
}
int main()
{
	int t;
	cin >> t;
	for (int i = 0; i < t; i++) {
		int n;
		cin >> n;
		TreeNode** p = new TreeNode * [n];
		for (int j = 0; j < n; j++) {
			int m;
			cin >> m;
			p[j] = new TreeNode(m);
		}
		for (int j = 0; j < n; j++) {
			char ch;
			cin >> ch;
			p[j]->ch = ch;
		}
		Sort_Tree(p, n);
		while (n > 1) {
			TreeNode* tmp = made(p[n - 1], p[n - 2]);
			p[n - 2] = tmp;
			n--;
			Sort_Tree(p, n);
		}
		cin >> n;
		for (int j = 0; j < n; j++) {
			string str;
			cin >> str;
			Decode(p[0], str);
		}
	}
	return 0;
}